//
//  NSDate+GFCalendar.m
//
//  Created by Mercy on 2016/11/9.
//  Copyright © 2016年 Mercy. All rights reserved.
//

#import "NSDate+GFCalendar.h"

@implementation NSDate (GFCalendar)

- (NSInteger)dateDay {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate:self];
    return components.day;
}

- (NSInteger)dateMonth {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:self];
    return components.month;
}

- (NSInteger)dateYear {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:self];
    return components.year;
}
-(NSInteger)dateWeek
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:self];
    return components.weekday;
}

- (NSDate *)previousMonthDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
    components.day = 15; // 定位到当月中间日子
    
    if (components.month == 1) {
        components.month = 12;
        components.year -= 1;
    } else {
        components.month -= 1;
    }
    
    NSDate *previousDate = [calendar dateFromComponents:components];
   
    return previousDate;
}

- (NSDate *)nextMonthDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
    components.day = 15; // 定位到当月中间日子
    
    if (components.month == 12) {
        components.month = 1;
        components.year += 1;
    } else {
        components.month += 1;
    }
    
    NSDate *nextDate = [calendar dateFromComponents:components];
    
    return nextDate;
}

- (NSInteger)totalDaysInMonth {
    NSInteger totalDays = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self].length;
    
    return totalDays;
}

- (NSInteger)firstWeekDayInMonth {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
    components.day = 1; // 定位到当月第一天
    NSDate *firstDay = [calendar dateFromComponents:components];
    
    // 默认一周第一天序号为 1 ，而日历中约定为 0 ，故需要减一
    NSInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDay] - 1;
    return firstWeekday;
}
//获取当前年 12个月份
- (NSDictionary *)monthsInyear
{
    NSMutableArray *months = [NSMutableArray array];
    NSMutableDictionary *yearDict = [NSMutableDictionary dictionary];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
    
    for (NSInteger i = 1; i<13; i++) {
        components.month = i;
        components.day = 15;
        NSDate *previousyearDate = [calendar dateFromComponents:components];
        [months addObject:previousyearDate];
    }
    [yearDict addEntriesFromDictionary:@{@"year":@(components.year)}];
    [yearDict addEntriesFromDictionary:@{@"month":months}];
    return yearDict;
    
}
- (NSDate *)previousYearDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
    components.month = 6;
    components.day = 15;
    components.year -= 1;
    NSDate *previousyearDate = [calendar dateFromComponents:components];
    return previousyearDate;
}
- (NSDate * )nextYearDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
    components.month = 6;
    components.day = 15;
    components.year += 1;
    NSDate *nextyearDate = [calendar dateFromComponents:components];
    return nextyearDate;
    
   
}

// 获取当前周的周一和周日的时间
- (NSString *)getWeekTime
{
   // NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:self];
    // 获取今天是周几
    NSInteger weekDay = [comp weekday];
    // 获取几天是几号
    NSInteger day = [comp day];
   // 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
    // 计算当前日期和本周的星期一和星期天相差天数
    long firstDiff,firstDiff1,firstDiff2,firstDiff3,firstDiff4,firstDiff5,lastDiff = 0;
     //  weekDay = 1;
    
    if (weekDay == 1)
    {
        firstDiff = -6;
        firstDiff1 = -5;
        firstDiff2 = -4;
        firstDiff3 = -3;
        firstDiff4 = -2;
        firstDiff5 = -1;
        lastDiff = 0;
    }else
    {
        firstDiff = [calendar firstWeekday] - weekDay + 1;
        firstDiff1 = 3- weekDay;
        firstDiff2 = 4- weekDay;
        firstDiff3 = 5- weekDay;
        firstDiff4 = 6- weekDay;
        firstDiff5 = 7- weekDay;
        lastDiff = 8 - weekDay;
    }
    NSLog(@"firstDiff: %ld   lastDiff: %ld ==%ld",firstDiff,lastDiff,weekDay);
    
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    [firstDayComp setDay:day ];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    NSDateComponents *firstDayComp1 = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    [firstDayComp1 setDay:day + 1];
    NSDate *firstDayOfWeek1 = [calendar dateFromComponents:firstDayComp1];
    
    NSDateComponents *firstDayComp2 = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    [firstDayComp2 setDay:day + 2];
    NSDate *firstDayOfWeek2 = [calendar dateFromComponents:firstDayComp2];
    
    NSDateComponents *firstDayComp3 = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    [firstDayComp3 setDay:day + 3];
    NSDate *firstDayOfWeek3 = [calendar dateFromComponents:firstDayComp3];
    
    NSDateComponents *firstDayComp4 = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    [firstDayComp4 setDay:day + 4];
    NSDate *firstDayOfWeek4 = [calendar dateFromComponents:firstDayComp4];
    
    NSDateComponents *firstDayComp5 = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    [firstDayComp5 setDay:day + 5];
    NSDate *firstDayOfWeek5 = [calendar dateFromComponents:firstDayComp5];
    
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:self];
    [lastDayComp setDay:day + 6];
    NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd"];
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    NSString *firstDay1 = [formatter stringFromDate:firstDayOfWeek1];
    NSString *firstDay2 = [formatter stringFromDate:firstDayOfWeek2];
    NSString *firstDay3 = [formatter stringFromDate:firstDayOfWeek3];
    NSString *firstDay4 = [formatter stringFromDate:firstDayOfWeek4];
    NSString *firstDay5 = [formatter stringFromDate:firstDayOfWeek5];
    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
    
    
    NSString *dateStr = [NSString stringWithFormat:@"%@+%@+%@+%@+%@+%@",firstDay,firstDay1,firstDay2,firstDay3,firstDay4,firstDay5];
    NSString *dateStr1 = [NSString stringWithFormat:@"%ld+%ld+%ld+%ld+%ld+%ld",[firstDayOfWeek dateWeek] ,[firstDayOfWeek1 dateWeek],[firstDayOfWeek2 dateWeek],[firstDayOfWeek3 dateWeek],[firstDayOfWeek4 dateWeek],[firstDayOfWeek5 dateWeek]];
    return [NSString stringWithFormat:@"%@=%@",dateStr,dateStr1];
    
}
//
- (NSString *)getMonthBeginAndEndWith{
    
        
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar setFirstWeekday:2];//设定周一为周首日
    BOOL ok = [calendar rangeOfUnit: NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:self];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
    }else {
        return @"";
    }
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"YYYY-MM-dd"];
   // NSString *beginString = [myDateFormatter stringFromDate:beginDate];
    NSString *endString = [myDateFormatter stringFromDate:endDate];
  //  NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];
    return endString;
}
- (NSString *)getMonthBeginAndEndWith1{
    
    
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar setFirstWeekday:2];//设定周一为周首日
    BOOL ok = [calendar rangeOfUnit: NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:self];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
    }else {
        return @"";
    }
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSString *beginString = [myDateFormatter stringFromDate:beginDate];
    NSString *endString = [myDateFormatter stringFromDate:endDate];
    NSString *s = [NSString stringWithFormat:@"%@+%@",beginString,endString];
    return s;
}

@end
